<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large" @keyup.enter="login">
		<el-form-item prop="user">
			<el-input v-model="form.user" prefix-icon="el-icon-user" clearable :placeholder="$t('login.userPlaceholder')">
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password
					  :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		<el-form-item style="margin-bottom: 10px;">
			<el-col :span="12">
				<el-checkbox :label="$t('login.rememberMe')" v-model="form.autologin"></el-checkbox>
			</el-col>
			<el-col :span="12" class="login-forgot">
				<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
			</el-col>
		</el-form-item>
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{
					$t('login.signIn')
				}}
			</el-button>
		</el-form-item>
	</el-form>
</template>

<script>
import {ElNotification} from "element-plus";

export default {
	data() {
		return {
			userType: 'admin',
			form: {
				user: "",
				password: "",
				autologin: false
			},
			rules: {
				user: [
					{required: true, message: this.$t('login.userError'), trigger: 'blur'}
				],
				password: [
					{required: true, message: this.$t('login.PWError'), trigger: 'blur'}
				]
			},
			islogin: false,
		}
	},
	watch: {
		userType(val) {
			if (val == 'admin') {
				this.form.user = 'admin'
				this.form.password = 'admin'
			} else if (val == 'user') {
				this.form.user = 'user'
				this.form.password = 'user'
			}
		}
	},
	mounted() {
		console.log('env------', process.env.NODE_ENV)
	},
	methods: {
		async login() {
			let validate = await this.$refs.loginForm.validate().catch(() => {
			})
			if (!validate) {
				return false
			}

			this.islogin = true
			let data = {
				username: this.form.user,
				password: this.form.password
			}
			await this.$API.auth.doLogin.post(data)
				.then((res) => {
					this.$TOOL.session.set("TOKEN", res.data['access_token'], {
						expires: this.form.autologin ? 24 * 60 * 60 : 0
					})
					this.$TOOL.data.set("USER_INFO", res.data['user_info'])
					this.$API['admin.menu'].power.get({'user_id': res.data['user_info']['user_id']})
						.then(menuRes => {
							this.$TOOL.data.set("MENU", menuRes.data['menu'])
							this.$TOOL.data.set("PERMISSIONS", menuRes.data['button'])
							this.$TOOL.data.set("DASHBOARDGRID", ["welcome", "ver", "time", "progress", "echarts", "about", "notice", "expiration"])
							this.$message.success(res.msg);
							this.$router.replace({
								path: '/'
							})
							this.islogin = false
						})
						.finally(() => {
							this.menuloading = false
						});
				})
				.catch((error) => {
					ElNotification.error({
						title: '请求错误',
						message: error || `Status:${error.response.status}，未知错误！`
					})
					this.islogin = false
				});
		},
	}
}
</script>

<style></style>
